/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.qihoo360.mobilesafe.utils;

import android.content.Context;
import android.os.SystemClock;
import android.text.TextUtils;
import java.io.File;
import java.io.IOException;

public class ZipUpdate {
    private static final String TAG = "ZipUpdate";
    private static final boolean DEBUG = 0;
    private static Context mContext;

    public ZipUpdate(Context paramContext) {
        mContext = paramContext;
    }

    public void copyAssetToFile() {
        delFilesSDK();
        unGzipToFiles();
        copyAssetTo("libclearsdk.gzip.ver");
    }

    public void copyNewToFile() {
        delFilesSDK();
        unGzipNewToFiles();
        renameFile("libclearsdk_new.gzip.ver", "libclearsdk.gzip.ver");
    }

    private void delFilesSDK() {
        deleteFolderFile(mContext.getFilesDir() + File.separator + "libclearsdk");
        delFile(mContext.getFilesDir() + File.separator + "libclearsdk.gzip" + ".ver");
        delFile(mContext.getFilesDir() + File.separator + "libcleardex.dex");
    }

    private void unGzipToFiles() {
        File localFile1 = new File(mContext.getFilesDir(), "libclearsdk");
        File localFile2 = new File(mContext.getFilesDir(), "libclearsdk.tmp");
        try {
            if (localFile2.exists())
                localFile2.delete();
            Utils.copyAssetsFile(mContext, "libclearsdk.gzip", localFile2);
            ZipUtil.unZip(localFile2, localFile1);
            if (!(localFile2.exists()))
                return;
            localFile2.delete();
        } catch (IOException localIOException) {
            localIOException.printStackTrace();
        }
    }

    private void unGzipNewToFiles() {
        File localFile1 = new File(mContext.getFilesDir(), "libclearsdk");
        File localFile2 = new File(mContext.getFilesDir(), "libclearsdk_new.gzip");
        try {
            if (!(localFile2.exists()))
                return;
            ZipUtil.unZip(localFile2, localFile1);
            localFile2.delete();
        } catch (IOException localIOException) {
            localIOException.printStackTrace();
        }
    }

    private void copyAssetTo(String paramString) {
        File localFile = new File(mContext.getFilesDir(), paramString);
        try {
            Utils.copyAssetsFile(mContext, paramString, localFile);
        } catch (IOException localIOException) {
            localIOException.printStackTrace();
        }
    }

    public void deleteFolderFile(String paramString) {
        if (TextUtils.isEmpty(paramString))
            return;
        File localFile = new File(paramString);
        if (!(localFile.exists()))
            return;
        File[] arrayOfFile;
        if (localFile.isDirectory()) {
            arrayOfFile = null;
            try {
                arrayOfFile = localFile.listFiles();
            } catch (Throwable localThrowable1) {
                System.gc();
                SystemClock.sleep(100L);
            }
            if (arrayOfFile == null)
                return;
            int i = arrayOfFile.length;
            for (int j = 0; j < i; ++j)
                deleteFolderFile(arrayOfFile[j].getAbsolutePath());
        }
        if (localFile.isDirectory()) {
            arrayOfFile = null;
            try {
                arrayOfFile = localFile.listFiles();
            } catch (Throwable localThrowable2) {
                System.gc();
                SystemClock.sleep(100L);
            }
            if (arrayOfFile == null)
                return;
            if (arrayOfFile.length != 0)
                return;
            localFile.delete();
        }
        else {
            localFile.delete();
        }
    }

    private void delFile(String paramString) {
        File localFile = new File(paramString);
        if (!(localFile.exists()))
            return;
        localFile.delete();
    }

    private void renameFile(String paramString1, String paramString2) {
        File localFile1 = new File(mContext.getFilesDir(), paramString1);
        File localFile2 = new File(mContext.getFilesDir(), paramString2);
        if (!(localFile1.exists()))
            return;
        localFile1.renameTo(localFile2);
    }
}